<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>07_猜字游戏</title>
</head>
<body>
<h4>已生成一个1~100随机数：</h4>
<input type="text" placeholder="请输入你的数字！">
<button onclick="c1()">点我验证</button>
<h4>小提示：<span></span></h4>
<script>
    let num=parseInt(Math.random()*100+1);
    let count=0;
    console.log(num);

    function c1(){
        let input=document.querySelector('input').value;
        if (!/^\d+$/.test(input)){
            alert('请输入数字')
        }
        console.log(typeof input);
        let n1=parseInt(input);
        let spanE=document.querySelector('span');
        count ++;
        if(n1>num){
            spanE.innerHTML="猜大了";

        }else if (n1<num){
            spanE.innerHTML="猜小了";
        }else{
            spanE.innerHTML="猜对了,共用了"+count+"次";
        }

    }
</script>
</body>
</html>